package com.pr.makfilm.service.impl;

import com.pr.makfilm.dao.IAbstractDao;
import com.pr.makfilm.dao.IDistrictDao;
import com.pr.makfilm.domain.District;
import com.pr.makfilm.domain.ObjectStateEnum;
import com.pr.makfilm.security.service.ISecurityService;
import com.pr.makfilm.service.IDistrictService;
import com.pr.makfilm.service.util.SidUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: pawel.radomski
 * Date: 07.12.13
 * Time: 12:52
 */
@Service(DistrictServiceImpl.BEAN_NAME)
public class DistrictServiceImpl extends AbstractServiceImpl<District> implements IDistrictService
{
   public static final String BEAN_NAME = "districtService";

   @Resource
   private IDistrictDao districtDao;

   @Resource
   private ISecurityService securityService;

   @Override
   protected IAbstractDao<District> getDao()
   {
      return districtDao;
   }

   @Override
   @Transactional
   public District save(District district)
   {
      district.setSid(SidUtils.generate());
      district.setObjectState(ObjectStateEnum.ACTIVE);
      district.setUser(securityService.getLoggedInUser());
      return super.save(district);
   }

   @Override
   @Transactional(readOnly = true)
   public List<District> findUserDistricts()
   {
      return districtDao.findByUser(securityService.getLoggedInUser());
   }
}
